<template>
  <div>
    <a-alert type="info" :showIcon="true" style="margin:20px 0 10px 0;">
      <div slot="message">
        该{{ type }}人员{{ pTitle1 }}评估规则：<span style="color:#1890FF;"> {{ alertInfo }}</span>
      </div>
    </a-alert>
    <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="false" :loading="loading" :columns="columns"
      :dataSource="pageData" @change="onTableChange">
      <template slot="evlNum" slot-scope="text">
        <span>第 {{ text }} 次</span>
      </template>
      <template slot="assessmentTime" slot-scope="text, record">
        <span>{{ record.assessmentStartTime }} ~ {{ record.assessmentEndTime }}</span>
      </template>
      <template slot="plannedTime" slot-scope="text, record">
        <span>{{ record.plannedStartTime }} ~ {{ record.plannedEndTime }}</span>
      </template>
      <template slot="assessmentStatus" slot-scope="text,record">
        <span v-if="text == '0'" style="color:#999">待开始</span>
        <span v-if="text == '1' && record.entryStatus == '2'" style="color:#108EE9">待审批</span>
        <span v-if="text == '2' && record.entryStatus == '2'" style="color:#FAAD14">审批中</span>
        <span v-if="text == '3'" style="color:#000">完成</span>
        <span v-if="text == '4'" style="color:#000">驳回</span>
        <span v-if="text == '5'" style="color:#999">已撤销</span>
        <span v-if="text == '9' && record.entryStatus == '2'" style="color:#27BF44">待录入</span>
        <span v-if="text == '10'" style="color:#FAAD14">补录申请中</span>
        <template v-if="record.entryStatus == '1'">
          <span v-if="text == '1' || text == '2'" style="color:#D9001B">已逾期 <a-tag color="red">领导</a-tag></span>
          <span v-if="text == '9'" style="color:#D9001B">已逾期 <a-tag color="red">专干</a-tag></span>
        </template>
      </template>
      <!--操作-->
      <template slot="action" slot-scope="text, record">
        <a-button v-if="record.assessmentStatus == '9' && record.entryStatus == '2'" @click="onInput(record)"
          type="link" style="padding: 0">添加评估</a-button>
        <template
          v-if="record.assessmentStatus == '1' || record.assessmentStatus == '2' || record.assessmentStatus == '10'">
          <a-button @click="onCX(record)" type="link" style="padding: 0">撤销申请</a-button>
          <a-divider type="vertical" />
          <a-button @click="onCYX(record)" type="link" style="padding: 0">催一下</a-button>
          <a-divider type="vertical" />
          <a-button @click="onDetail(record)" type="link" style="padding: 0">查看</a-button>
          <a-divider type="vertical" />
        </template>
        <template v-if="record.entryStatus == '2' && record.assessmentStatus == '3'">
          <a-button @click="onDetail(record)" type="link" style="padding: 0">查看</a-button>
        </template>
        <template v-if="record.entryStatus == '1' && record.assessmentStatus == '9'">
          <a-button @click="onInput(record)" type="link" style="padding: 0">评估补录</a-button>
          <a-divider type="vertical" />
        </template>
        <a-button v-if="record.assessmentStatus == '4' || record.assessmentStatus == '5'" @click="onInput(record)"
          type="link" style="padding: 0">重新提交</a-button>
      </template>
    </z-table>
    <AddModal ref="addModalId" :pTitle="pTitle" @ok="onQuery" />
  </div>
</template>
<script>
import * as totalEstimate from '@/api/sjsk/help/totalEstimate.js'
import AddModal from './AddModal'
import { getUrge } from '@/api/sjsk/flowService'
export default {
  components: {
    AddModal,
  },
  data() {
    return {
      loading: false,
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '评估次数',
          dataIndex: 'evlNum',
          align: 'center',
          scopedSlots: { customRender: 'evlNum' },
        },
        {
          title: '评估时段',
          dataIndex: 'assessmentTime',
          align: 'center',
          scopedSlots: { customRender: 'assessmentTime' },
        },
        {
          title: '计划时间',
          dataIndex: 'plannedTime',
          align: 'center',
          scopedSlots: { customRender: 'plannedTime' }
        },
        {
          title: '录入状态',
          dataIndex: 'assessmentStatus',
          align: 'center',
          scopedSlots: { customRender: 'assessmentStatus' },
        },
        {
          title: '评估时间',
          dataIndex: 'createTime',
          align: 'center',
        },
        {
          title: '操作',
          width: 250,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      recordData: JSON.parse(this.$route.query.record),
      // personId: this.$route.query.personId,
      assessmentType: 1, //评估类型，1为季度评估，2为年度评估
      pageData: [],
      alertInfo: "",
      pTitle: "添加季度评估",
      pTitle1: "季度",
      type: '社戒'
    }
  },
  created() {
    // this.onQuery()
    if (this.recordData.type == '1') {
      this.type = '社戒'
    } else {
      this.type = '社康'
    }
  },
  methods: {
    onQuery() {
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      let params = Object.assign({}, { id: this.recordData.personId, type: this.assessmentType })
      totalEstimate.viewCompEvaluationHistory(params).then((res) => {
        if (res.success) {
          if (res.result != null) {
            this.alertInfo = res.result.time
            this.pageData = res.result.viewCompEvaluationHistoryVOS
          }
          this.loading = false
        } else {
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    setType(type) {
      this.assessmentType = type
      if (this.assessmentType == '1') {
        this.pTitle1 = "季度"
      } else {
        this.pTitle1 = "年度"
      }
    },
    onInput(record) {
      if (this.assessmentType == '1') {
        this.pTitle = "添加季度评估"
      } else {
        this.pTitle = "添加年度评估"
      }
      //人员的基本信息
      let data1 = {
        personId: Number(this.recordData.personId),
        name: this.recordData.name,
        type: this.recordData.type,
        identification: this.recordData.identification,
        sex: this.recordData.sex,
        currentAddress: this.recordData.currentAddress,
        caseNo: this.recordData.caseNo
      }
      //历史记录的信息
      let data2 = {
        id: record.id,
        assessmentType: this.assessmentType,
        status: record.assessmentStatus,
        entryStatus: record.entryStatus,
        evaluationStartTime: record.assessmentStartTime,
        evaluationEndTime: record.assessmentEndTime
      }
      let params = { ...data1, ...data2 }
      this.$refs.addModalId.show(params)
    },
    //跳到审批详情
    onDetail(record) {
      let flowType = ""
      if (record.assessmentStatus == '10') { //补录申请中
        if (this.assessmentType == '1') flowType = "28" //季度评估补录审批
        if (this.assessmentType == '2') flowType = "29" //年度评估补录审批
      } else { //待审批，审批中
        if (this.assessmentType == '1') flowType = "26" //季度评估审批
        if (this.assessmentType == '2') flowType = "27" //年度评估审批
      }
      this.$router.push({
        path: `/examine/details/` + flowType + `/${record.id}`
      })
    },
    onCYX(record) {
      getUrge({ taskId: record.id, taskType: '19' }).then((res) => { //取消常规尿液检测审批
        if (res.success) {
          this.$message.success(res.message);
          this.onQuery()
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    onCX(record) {
      let that = this;
      let flowType = ""
      if (record.assessmentStatus == '10') { //补录申请中
        if (this.assessmentType == '1') flowType = "28" //季度评估补录审批
        if (this.assessmentType == '2') flowType = "29" //年度评估补录审批
      } else { //待审批，审批中
        if (this.assessmentType == '1') flowType = "26" //季度评估审批
        if (this.assessmentType == '2') flowType = "27" //年度评估审批
      }
      this.$confirm({
        title: `还在审批中，确定要撤销吗?`,
        okText: "确认",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          totalEstimate.compEvaluationRevoke({ id: record.id, type: flowType }).then((res) => { //取消定期访谈审批，审批流程类型24
            if (res.success) {
              that.$message.success(res.message);
              that.onQuery()
            } else {
              that.$message.warning(res.message);
            }
          });
        }
      });
    },
  },
}
</script>
<style lang="less" scoped>

</style>
